package com.edu.service.impl;

import com.edu.constant.MessageConstant;
import com.edu.dao.MemberDao;
import com.edu.dao.OrderDao;
import com.edu.dao.TOrdersettingMapper;
import com.edu.entity.Result;
import com.edu.pojo.Member;
import com.edu.pojo.Order;
import com.edu.pojo.TOrdersetting;
import com.edu.pojo.TOrdersettingExample;
import com.edu.service.OrderService;
import com.edu.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private TOrdersettingMapper tOrdersettingMapper;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Result order(Map map) throws Exception {
        // 检查用户所选择的日期是否已经提前进行了预约位置，如果没有位置则无法进行预约
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        TOrdersettingExample tOrdersettingExample = new TOrdersettingExample();
        tOrdersettingExample.createCriteria().andOrderdateEqualTo(date);
        List<TOrdersetting> tOrdersettings = tOrdersettingMapper.selectByExample(tOrdersettingExample);
        TOrdersetting tOrdersetting = tOrdersettings.get(0);
        if (tOrdersetting == null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        // 检查用户所选择的预约日期是否已经约满了，如果约满则无法进行预约
        Integer number = tOrdersetting.getNumber();
        Integer reservations = tOrdersetting.getReservations();
        if (reservations >= number){
            return new Result(false,MessageConstant.ORDER_FULL);
        }

        // 检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再次预约
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);

        if(member != null){
            // 判断用户是否重复预约
            Integer id = member.getId();
            String setmealId = (String) map.get("setmealId");
            Order order = new Order(id, date, Integer.parseInt(setmealId));
            List<Order> list = orderDao.findByCondition(order);
            if (list != null && list.size() > 0){
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }else {
            // 检查当前用户是否为会员，如果是会员直接完成预约，如果不是会员自动注册完成预约
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);
        }
        // 预约成功后，更新当日已预约人数
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(date);
        order.setOrderType((String) map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
        orderDao.add(order);
        tOrdersetting.setReservations(tOrdersetting.getReservations()+1);
        tOrdersettingMapper.updateByPrimaryKeySelective(tOrdersetting);
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById4Detail(id);

        if (map != null){
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
